package com.example.sunmiv1.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.TodayRecordAdapter;
import com.example.sunmiv1.entity.TodayVipInfo;
import com.example.sunmiv1.entity.VipRecordBean;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipRecordActivity extends Activity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.vipRecord_search_edit)
    EditText vipRecordSearchEdit;
    @BindView(R.id.vipRecord_search_iv)
    ImageView vipRecordSearchIv;
    @BindView(R.id.vipRecord_today_recharge)
    TextView vipRecordTodayRecharge;
    @BindView(R.id.vipRecord_actual_recharge)
    TextView vipRecordActualRecharge;
    @BindView(R.id.vipRecord_give_recharge)
    TextView vipRecordGiveRecharge;
    @BindView(R.id.vipRecord_today_consume)
    TextView vipRecordTodayConsume;
    @BindView(R.id.vipRecord_consume_people)
    TextView vipRecordConsumePeople;
    @BindView(R.id.vipRecord_average_consume)
    TextView vipRecordAverageConsume;
    @BindView(R.id.vipRecord_listView)
    PullToRefreshListView vipRecordListView;
    @BindView(R.id.vipRecord_activity_add)
    TextView vipRecordActivityAdd;
    @BindView(R.id.vipRecord_refund_money)
    TextView vipRecordRefundMoney;

    private Context context;
    private ACache aCache;
    private LoadingDialog loadingDialog;
    private List<VipRecordBean.DataBean> list;
    private TodayVipInfo todayVipInfo;
    private VipRecordBean vipRecordBean;
    private int page = 1;
    private TodayRecordAdapter todayRecordAdapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Toast.makeText(context, todayVipInfo.getMsg(), Toast.LENGTH_SHORT).show();
                    vipRecordTodayRecharge.setText(todayVipInfo.getAllmoney() + "");
                    vipRecordTodayConsume.setText(todayVipInfo.getPay() + "");
                    vipRecordActualRecharge.setText(todayVipInfo.getSummoney() + "");
                    vipRecordConsumePeople.setText(todayVipInfo.getPaynum() + "");
                    vipRecordGiveRecharge.setText(todayVipInfo.getResummoney() + "");
                    vipRecordAverageConsume.setText(todayVipInfo.getAve() + "");
                    vipRecordActivityAdd.setText(todayVipInfo.getHdadd() + "");
                    vipRecordRefundMoney.setText(todayVipInfo.getRefund() + "");
                    break;
                case 2:
                    Toast.makeText(context, todayVipInfo.getMsg(), Toast.LENGTH_SHORT).show();
                    vipRecordTodayRecharge.setText("0");
                    vipRecordTodayConsume.setText("0");
                    vipRecordActualRecharge.setText("0");
                    vipRecordConsumePeople.setText("0");
                    vipRecordGiveRecharge.setText("0");
                    vipRecordAverageConsume.setText("0");
                    vipRecordActivityAdd.setText("0");
                    vipRecordRefundMoney.setText("0");
                    break;
                case 3:
                    if (page == 1) {
                        list.clear();
                    }
                    page++;
                    for (int i = 0; i < vipRecordBean.getData().size(); i++) {
                        list.add(vipRecordBean.getData().get(i));
                    }
                    setTodayRecordAdapter();
                    break;
                case 4:
                    setTodayRecordAdapter();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_vip_record);
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.vipRecordTtile);
        initData();
    }

    private void initData() {
        list = new ArrayList<>();
        vipRecordListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                list.clear();
                getVipRecord();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getVipRecord();
            }
        });
        getRecord();
        getVipRecord();
    }

    private void setTodayRecordAdapter() {
        if (todayRecordAdapter == null) {
            todayRecordAdapter = new TodayRecordAdapter(context, list);
            vipRecordListView.setAdapter(todayRecordAdapter);
        } else {
            todayRecordAdapter.notifyDataSetChanged();
        }
    }

    @OnClick({R.id.title_back, R.id.vipRecord_search_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.vipRecord_search_iv:
                getRecord();
                getVipRecord();
                break;
        }
    }

    private void getRecord() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            jsonObject.put("keyword", vipRecordSearchEdit.getText().toString());
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            HttpUtils.post(context, Common.GET_TODAY_VIP_CARD_INFO, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    todayVipInfo = GsonUtils.JsonToBean(text, TodayVipInfo.class);
                    Message msg = new Message();
                    if (todayVipInfo.getStatus() == 1) {
                        msg.what = 1;
                    } else {
                        msg.what = 2;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private static final String TAG = "VipRecordActivity";

    private void getVipRecord() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("keyword", vipRecordSearchEdit.getText().toString().trim());
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            jsonObject.put("page", page);
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            HttpUtils.post(context, Common.GET_TODAY_VIP_RECORD, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    vipRecordListView.onRefreshComplete();
                    vipRecordBean = GsonUtils.JsonToBean(text, VipRecordBean.class);
                    Message msg = new Message();
                    if (vipRecordBean.getStatus() == 1) {
                        msg.what = 3;
                    } else {
                        msg.what = 4;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                    vipRecordListView.onRefreshComplete();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
